<?php


namespace App\Models\App;


use App\Commons\Utils\ModelTools\BaseModel;
use App\Commons\WechatOpenPlatform\WechatOpenPlatformTool;

class AppProjectType extends BaseModel {
    protected $dateFormat = 'Y-m-d H:i:s';

    protected $searchable = [
        'name',
        'created_at',
    ];

    //是否删除
    const DELETED_NO  = 0;
    const DELETED_YES = 1;

    /**
     * @param $query
     * @return mixed
     */
    public function scopeNoDeleted($query) {
        return $query->where('deleted', self::DELETED_NO);
    }

    /**
     * 对ext_json操作
     * @param $val
     */
    public function setExtJsonAttribute($val) {
        $extJson = '{}';
        if (is_array($val) && !empty($val)) {
            $extJson = json_encode($val);
        }

        $this->attributes['ext_json'] = $extJson;
    }

    public function getExtJsonAttribute($val) {
        $extJsonList = json_decode($val, true);

        return is_array($extJsonList) ? $extJsonList : [];
    }

    /**
     * server_domain
     * @param $val
     */
    public function setServerDomainAttribute($val) {
        if (is_array($val)) {
            $serverDomain = json_encode($val);
        } else {
            $serverDomain = json_encode(WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonServerDomain());
        }

        $this->attributes['server_domain'] = $serverDomain;
    }

    public function getServerDomainAttribute($val) {
        $extJsonList = json_decode($val, true);

        return is_array($extJsonList) ? $extJsonList : WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonServerDomain();
    }

    /**
     * webviewdomain
     * @param $val
     */
    public function setWebviewdomainAttribute($val) {
        if (is_array($val)) {
            $serverDomain = json_encode($val);
        } else {
            $serverDomain = json_encode(WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain());
        }

        $this->attributes['webviewdomain'] = $serverDomain;
    }

    public function getWebviewdomainAttribute($val) {
        $extJsonList = json_decode($val, true);

        return is_array($extJsonList) ? $extJsonList : WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain();
    }

    /**
     * wx_submit_audit_item_list
     * @param $val
     */
    public function setWxSubmitAuditItemListAttribute($val) {
        if (is_array($val)) {
            $jsonRes = json_encode($val);
        } else {
            $jsonRes = json_encode(WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain());
        }

        $this->attributes['wx_submit_audit_item_list'] = $jsonRes;
    }

    public function getWxSubmitAuditItemListAttribute($val) {
        $arrRes = json_decode($val, true);

        return is_array($arrRes) ? $arrRes : [];
    }
}